import { useRef } from 'react';
import FormStore from '../FormStore';
import type { FormInstance } from '../interface';

//每次调用Form.useForm都会new 一个新的实例
function useForm<Values = any>(form?: FormInstance<Values>) {
  const formRef = useRef<FormInstance>();
  if (!formRef.current) {
    if (form) {
      formRef.current = form;
    } else {
      formRef.current = new FormStore().getForm();
    }
  }

  return [formRef.current];
}

export default useForm;
